using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Oplaco.Web.DTO;
using Oplaco.Web.UI;
using System.Web;
using Oplaco.Core.BusinessLogicLayer;


public partial class UserAdvertismentsGrid_ascx : System.Web.UI.UserControl
{
    private Oplaco.Core.Configuration.SiteSettings settings = Oplaco.Core.Configuration.SiteSettings.GetSharedSettings();

    public String CurrentNavigatieUrl
    {
        get;
        set;
    }

    public bool IsSecure
    {
        get;
        set;
    }
    

    /// <summary>
    /// Loads data for the control. If an categoryid is set, it tries to load ads based on the categoryid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentNavigatieUrl = HttpContext.Current.Request.Url.AbsolutePath + "?cid=";
        int catId = 0;

        if (Page.Request.QueryString.Count > 0) {            
            catId = Convert.ToInt32(Page.Request.QueryString["cid"]);
        }
               
                      
        CachedAdvertismentCollection alist = null;
        this.loadCategoryNavigation();
        AdsList.Visible = true;

        if (this.IsSecure && Profile.MemberId > 0) {

            alist = this.loadAdvertisments(catId, Profile.MemberId, 100);            
        }
        else
        {
            alist = this.loadAdvertisments(catId, 0, 100);            
        }


        AdsList.DataSource = alist;
        AdsList.DataBind();   
        
    }

    

    private void loadCategoryNavigation() {
        //load all categories
        CachedCategoryCollection clist = CategoryManager.Fetch(HttpContext.Current, 100);
        if (clist != null && clist.Count > 0)
        {
            CategoryNavigation.DataSource = clist;
            CategoryNavigation.DataBind();
        }
    
    }

    private CachedAdvertismentCollection loadAdvertisments(int categoryId, int profileMemberId, int maximumItems) {

        return AdvertismentManager.FetchByQuery(categoryId, profileMemberId, 10000000, String.Empty, AdType.Unspecified, AdLevel.Unspecified, settings.MaxAdPrice, false, maximumItems);
    }
}
